package 数据库.day2;
import java.sql.*;

/**
 * @author oldliu
 * @since 1.0
 */
public class DBUtil {
    public static final String clsName="com.mysql.cj.jdbc.Driver";
    public static final String url="jdbc:mysql://localhost:3306/初体验?useSSL=false&characterEncoding=UTF-8&serverTimezone=GMT%2b8";
    public static final String user="root";
    public static final String pass="123123";
    private DBUtil(){
    }
    static{
        try {
            Class.forName(clsName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, user, pass);
    }

//    public static void execute(String sql)  {
//        PreparedStatement ps=null;
//        try {
//            con = getConnection();
//            con.setAutoCommit(false);
//            ps = con.prepareStatement(sql);
//            ps.executeUpdate();
//        }catch (Exception e){
//            e.printStackTrace();
//        }finally {
//            CloseAll(null,ps,con);
//        }
//    }

    public static void  close(ResultSet res, Statement sta,Connection con)  {
        close(res);
        close(sta);
        close(con);
    }
    public static void close(ResultSet rs){
        if(rs!=null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void close(Statement st){
        if(st!=null) {
            try {
               //st.setMaxRows(0);
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void close(Connection con){
            if(con!=null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
    }

}
